package com.jyb.qiyefuwu.view.fragment.usercenter;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;

import com.jyb.qiyefuwu.R;
import com.jyb.qiyefuwu.databinding.FragmentDatabaseListBinding;
import com.jyb.qiyefuwu.entity.DatabaseListEntiy;
import com.jyb.qiyefuwu.uitls.DialogFunction;
import com.jyb.qiyefuwu.uitls.DialogHelper;
import com.jyb.qiyefuwu.view.activity.usercenter.BillingRecordActivity;
import com.jyb.qiyefuwu.view.adpater.DatabaseInfoAdapter;
import com.jyb.qiyefuwu.viewmodel.DatabaseListModel;
import com.jyb.qiyefuwu.viewmodel.SalesOderDetailsModel;
import com.swgk.core.base.AppLazyFragment;
import com.swgk.core.customview.PhotoViewActivity;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.util.MToast;
import com.swgk.core.xrecyclerview.XRecyclerView;

import org.jetbrains.annotations.NotNull;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import static com.swgk.core.common.base.BaseViewModel.SHOW_LODING;


/**
 * @author: 陈方凯
 * @date: 2021/09/17
 * @Description: 资料库列表
 */
public class DatabaseListFragment extends AppLazyFragment {

    private DatabaseListModel mModel;
    private FragmentDatabaseListBinding mBinding;
    private DatabaseInfoAdapter mAdapter;
    private boolean isLodMore;
    private boolean isGone;
    private int isBuyers;
    private String orderId;
    private Intent intent;
    private List<String> urls = new ArrayList<>();

    public static DatabaseListFragment newInstance(int isBuyers, String orderId, boolean isGone) {
        DatabaseListFragment fragment = new DatabaseListFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("isBuyers", isBuyers);
        bundle.putString("orderId", orderId);
        bundle.putBoolean("isGone", isGone);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @org.jetbrains.annotations.Nullable
    @Override
    public View onCreateView(@NonNull @NotNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_database_list, container, false);
        mModel = new ViewModelProvider(this).get(DatabaseListModel.class);
        isBuyers = getArguments().getInt("isBuyers");
        orderId = getArguments().getString("orderId");
        isGone = getArguments().getBoolean("isGone");
//        orderId = "785";
        initAdapter();
        initObserver();
        return mBinding.getRoot();
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }


    @Override
    public void onClick(View v) {

    }

    private void initAdapter() {
        mAdapter = new DatabaseInfoAdapter(getActivity(), null, isGone);
        mAdapter.setItemSelect(false);
        mBinding.rvList.setLayoutManager(new GridLayoutManager(getActivity(), 2));
        mBinding.rvList.setAdapter(mAdapter);
        mAdapter.onDelete(new DatabaseInfoAdapter.IDelete() {
            @Override
            public void delete(String id) {
                //TODO 删除操作
                isLodMore = false;
                DialogHelper.showDialog(getActivity(), "提示", "是否确认删除？",
                        "取消", "确定", new DialogFunction() {
                            @Override
                            public void normalRoundDialog() {
                                mModel.delDatabase(id);
                            }
                        });

            }

            @Override
            public void lookPdf(String path) {
                //TODO 点击跳转浏览器
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_VIEW);
                Uri content_url = Uri.parse(path);
                intent.setData(content_url);
                startActivity(intent);
            }

            @Override
            public void lookImg(String path) {
                //TODO 查看图片
                urls.clear();
                urls.add(path);
                openPhotoView(0);
            }
        });
        mBinding.rvList.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                //TODO 刷新
                isLodMore = false;
                mModel.getDatabaseList(isBuyers, isLodMore, orderId);
            }

            @Override
            public void onLoadMore() {
                //TODO 加载更多
                isLodMore = true;
                mModel.getDatabaseList(isBuyers, isLodMore, orderId);
            }
        });
    }

    private void initObserver() {
        mModel.listData.observe(getActivity(), listdata -> {
            if (isLodMore) {
                if (null != listdata && listdata.size() > 0) {
                    mBinding.rvList.loadMoreComplete();
                    mAdapter.addDatas(listdata);
                } else {
                    mBinding.rvList.setNoMore(true);
                }
                mBinding.rvList.setVisibility(View.VISIBLE);
            } else {
                mBinding.rvList.refreshComplete();
                if (listdata.size() == 0) {
                    mAdapter.setDatasMayNull(listdata);
                    mBinding.errorView.setVisibility(View.VISIBLE);
                } else {
                    mBinding.errorView.setVisibility(View.GONE);
                    mBinding.rvList.setVisibility(View.VISIBLE);
                    mAdapter.setDatas(listdata);
                }
            }
        });
        mModel.delOk.observe(getActivity(), new Observer<String>() {
            @Override
            public void onChanged(String s) {
                //刷新数据
                mModel.getDatabaseList(isBuyers, isLodMore, orderId);
            }
        });
        mModel.delFail.observe(getActivity(), new Observer<String>() {
            @Override
            public void onChanged(String s) {
                //刷新数据
                mModel.getDatabaseList(isBuyers, isLodMore, orderId);
            }
        });
        mModel.toastMsg.observe(getActivity(), new Observer<String>() {
            @Override
            public void onChanged(String s) {
                MToast.showToast(getActivity(), s);
            }
        });
        mModel.mLoading.observe(getActivity(), new Observer<Integer>() {
            @Override
            public void onChanged(Integer integer) {
                if (integer == SHOW_LODING) {
                    com.swgk.core.dialog.DialogHelper.showProgressDialog(getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
                } else {
                    com.swgk.core.dialog.DialogHelper.dismissProgressDialog();
                }
            }
        });
    }

    @Override
    public boolean isUseEventBus() {
        return true;
    }

    @Override
    protected void onLazyLoad() {
        mModel.getDatabaseList(isBuyers, isLodMore, orderId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    /**
     * 预览
     **/
    private void openPhotoView(int position) {
        if (intent == null) {
            intent = new Intent();
        }
        intent.setClass(getActivity(), PhotoViewActivity.class);
        intent.putExtra(PhotoViewActivity.KEY_PHOTO_URLS, (Serializable) urls);
        intent.putExtra(PhotoViewActivity.KEY_PHOTO_POSITION, position);
        getActivity().startActivity(intent);
    }
}
